package com.heima.article.listener;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.heima.article.mapper.ArticleConfigMapper;
import com.heima.model.article.pojos.ApArticleConfig;
import com.heima.model.common.constants.WmNewsMessageConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
public class DownOrUpListener {

    @Autowired
    private ArticleConfigMapper articleConfigMapper;
    /*
    监听文章上下架功能
     */
    @KafkaListener(topics = WmNewsMessageConstants.WM_NEWS_UP_OR_DOWN_TOPIC)
    public void getMessage(String  message){
        //1.转成对象
        Map map = JSONObject.parseObject(message, Map.class);
        //TODO
        Integer enable = (Integer) map.get("enable");
        //2.修改ap_article_config表中的状态值
        ApArticleConfig entity=articleConfigMapper.selectOne(Wrappers.<ApArticleConfig>lambdaQuery().eq(ApArticleConfig::getArticleId,map.get("articleId")));
        if(entity!=null){
            /**
             * article-config表中的is-down =1 表示下架，==0表示上架
             * map.get("enable") ==1表示上架，==0表示下架
             */
            entity.setIsDown(enable==1?false:true);
            articleConfigMapper.updateById(entity);
        }

    }
}
